package patrol.mobile.com.mobilepatrol.activity.yanghuke.lukuangxinxi;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.LineChartBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.bean.YhkRoutIndexBean;
import patrol.mobile.com.mobilepatrol.bean.YhkRouteBean;
import patrol.mobile.com.mobilepatrol.bean.YhkYearBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.LineChartUtil;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by z on 2018/9/9.
 * Des: 路况信息
 */

public class LuKuangXinXiActivity extends BaseActivity {
    @BindView(R.id.mySpinner01)
    MySpinner mySpinner01;
    @BindView(R.id.mySpinner02)
    MySpinner mySpinner02;
    @BindView(R.id.mySpinner03)
    MySpinner mySpinner03;
    @BindView(R.id.mLineChart)
    LineChart mLineChart;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    private OrgListBean orgListBean;
    private YhkYearBean yhkYearBean;
    private YhkRouteBean yhkRouteBean;
    private YhkRoutIndexBean yhkRoutIndexBean;
    private int danweiPostion;
    private int yearPostion;
    private int routePostion;
    private LuKxxAdapter luKxxAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_lukuangxinxi;
    }

    @Override
    public void initView() {
        super.initView();
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("路况数据");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(luKxxAdapter = new LuKxxAdapter());
        getHttpData();
    }


    @Override
    public void setListener() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    /**
     * 获取机构
     */
    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                getHttpDataYear();
                orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                mySpinner01.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        danweiPostion = postion;
                        if (yhkYearBean != null) {
                            getRouteList(orgListBean.getList().get(danweiPostion).getId(), yhkYearBean.getList().get(yearPostion).getId());
                        }
                    }
                });
            }
        });
    }

    /**
     * 获取年份
     */
    private void getHttpDataYear() {
        HashMap<String, String> hashMap = new HashMap();
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getAccYearList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                yhkYearBean = new Gson().fromJson(response, YhkYearBean.class);
                List<String> list = new ArrayList<>();
                for (YhkYearBean.ListBean listBean : yhkYearBean.getList()) {
                    list.add(listBean.getName());
                }
                mySpinner03.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        yearPostion = postion;
                        getRouteList(orgListBean.getList().get(danweiPostion).getId(), yhkYearBean.getList().get(yearPostion).getId());
                    }
                });
                getRouteList(orgListBean.getList().get(danweiPostion).getId(), yhkYearBean.getList().get(yearPostion).getId());
            }
        });
    }

    /**
     * 获取路线
     */
    private void getRouteList(String orgId, String yearId) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("orgId", orgId);
        hashMap.put("yearId", yearId);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getRouteList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                yhkRouteBean = new Gson().fromJson(response, YhkRouteBean.class);
                List<String> list = new ArrayList<>();
                for (YhkRouteBean.ListBean listBean : yhkRouteBean.getList()) {
                    list.add(listBean.getRouteName());
                }
                mySpinner02.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        if (null == yhkRouteBean.getList() || 0 == yhkRouteBean.getList().size()) {
                            ToastUtils.showToast("暂无数据");
                            return;
                        }
                        routePostion = postion;
                        yearRouteIndexTableData(orgListBean.getList().get(danweiPostion).getId(),
                                yhkYearBean.getList().get(yearPostion).getCode(),
                                yhkRouteBean.getList().get(routePostion).getId());
                    }
                });
                if (null == yhkRouteBean.getList() || 0 == yhkRouteBean.getList().size()) {
                    ToastUtils.showToast("暂无数据");
                    return;
                }
                yearRouteIndexTableData(orgListBean.getList().get(danweiPostion).getId(),
                        yhkYearBean.getList().get(yearPostion).getCode(),
                        yhkRouteBean.getList().get(0).getId());
            }
        });
    }

    /**
     * 获取路线
     */
    private void yearRouteIndexTableData(String orgId, String year, String routeId) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("orgId", orgId);
        hashMap.put("year", year);
        hashMap.put("routeId", routeId);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.yearRouteIndexTableData);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                yhkRoutIndexBean = new Gson().fromJson(response, YhkRoutIndexBean.class);
                if (yhkRoutIndexBean.getList() == null || yhkRoutIndexBean.getList().size() == 0) {
                    ToastUtils.showToast("数据为空");
                    return;
                }
                LineChartUtil.setLineChart02(mLineChart, getLineChartList(yhkRoutIndexBean), "路况信息", getResources().getColor(R.color.red));
                Log.e("~~~~", yhkRoutIndexBean.getList().size() + "_____");
                luKxxAdapter.setYhkRoutIndexBean(yhkRoutIndexBean);
            }
        });
    }

    private List<LineChartBean> getLineChartList(YhkRoutIndexBean yhkRoutIndexBean) {
        List<LineChartBean> lineChartList = new ArrayList<>();
        for (int i = 0; i < yhkRoutIndexBean.getList().size(); i++) {
            LineChartBean lineChartBean = new LineChartBean();
            lineChartBean.setValue((long) Float.parseFloat(yhkRoutIndexBean.getList().get(i).getPci()));
            lineChartBean.setName(yhkRoutIndexBean.getList().get(i).getPotkm());
            lineChartList.add(lineChartBean);
        }
        return lineChartList;
    }

    public class LuKxxAdapter extends RecyclerView.Adapter<LuKxxAdapter.ViewHolder> {
        YhkRoutIndexBean yhkRoutIndexBean;

        public void setYhkRoutIndexBean(YhkRoutIndexBean yhkRoutIndexBean) {
            this.yhkRoutIndexBean = yhkRoutIndexBean;
            notifyDataSetChanged();
        }

        @Override
        public LuKxxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lkxx_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(LuKxxAdapter.ViewHolder holder, int position) {
            holder.fangxiangText.setText(yhkRoutIndexBean.getList().get(position).getDirection());
            holder.zhuanghaoText.setText(yhkRoutIndexBean.getList().get(position).getName());

            holder.pqlText.setText(yhkRoutIndexBean.getList().get(position).getPqi());

            holder.pcl02Text.setText(yhkRoutIndexBean.getList().get(position).getRqi());

            holder.pclText.setText(yhkRoutIndexBean.getList().get(position).getPci());

            holder.pqlText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.white));
            holder.pclText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.white));
            holder.pcl02Text.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.white));
            if (!TextUtils.isEmpty(yhkRoutIndexBean.getList().get(position).getPqi())) {
                if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPqi()) < 60) {
                    holder.pqlText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer01));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPqi()) <= 70) {
                    holder.pqlText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer02));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPqi()) <= 80) {
                    holder.pqlText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer03));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPqi()) <= 90) {
                    holder.pqlText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer04));
                } else {
                    holder.pqlText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer05));
                }
            }
            if (!TextUtils.isEmpty(yhkRoutIndexBean.getList().get(position).getPci())) {
                if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPci()) < 60) {
                    holder.pclText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer01));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPci()) <= 70) {
                    holder.pclText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer02));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPci()) <= 80) {
                    holder.pclText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer03));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getPci()) <= 90) {
                    holder.pclText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer04));
                } else {
                    holder.pclText.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer05));
                }
            }
            if (!TextUtils.isEmpty(yhkRoutIndexBean.getList().get(position).getRqi())){
                if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getRqi()) < 60) {
                    holder.pcl02Text.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer01));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getRqi()) <= 70) {
                    holder.pcl02Text.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer02));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getRqi()) <= 80) {
                    holder.pcl02Text.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer03));
                } else if (Double.parseDouble(yhkRoutIndexBean.getList().get(position).getRqi()) <= 90) {
                    holder.pcl02Text.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer04));
                } else {
                    holder.pcl02Text.setBackgroundColor(holder.itemView.getContext().getResources().getColor(R.color.lukuang_coloer05));
                }
            }

        }

        @Override
        public int getItemCount() {
            return yhkRoutIndexBean == null ? 0 : yhkRoutIndexBean.getList().size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.zhuanghaoText)
            TextView zhuanghaoText;
            @BindView(R.id.fangxiangText)
            TextView fangxiangText;
            @BindView(R.id.pqlText)
            TextView pqlText;
            @BindView(R.id.pclText)
            TextView pclText;
            @BindView(R.id.pcl02Text)
            TextView pcl02Text;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }


    }
}
